package com.zhiqingchun.web.user;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zhiqingchun.dao.account.IUserDao;
import com.zhiqingchun.dao.account.UserResult;
import com.zhiqingchun.dao.article.IArticleDao;
import com.zhiqingchun.dao.comment.ICommentDao;
import com.zhiqingchun.dao.contact.ContactResult;
import com.zhiqingchun.dao.contact.IContactDao;
import com.zhiqingchun.pojo.ArticleDO;
import com.zhiqingchun.pojo.CommentDO;
import com.zhiqingchun.pojo.Usr;
import com.zhiqingchun.pojo.UsrContactDO;

@Controller
@RequestMapping("/user.html")
public class UserController {

	@RequestMapping(method = RequestMethod.GET)
	public String user(ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		String _id = request.getParameter("id");
		int id = 0;
		if (StringUtils.isNotBlank(_id)) {
			id = Integer.parseInt(_id);
		}
		UserResult userResult = userDao.byId(id);
		if (userResult.isSuccess()) {
			map.addAttribute("userInfo", userResult.getUsr());
		}
		
		ArticleDO articleDO = new ArticleDO();
		articleDO.setAuthor(id);
		List<ArticleDO> articleList = articleDao.list(articleDO, null);
		if (articleList.size() > 5) {
			articleList = articleList.subList(0, 5);
		}
		map.addAttribute("articleList", articleList);
		
		CommentDO commentDO = new CommentDO();
		commentDO.setUsrId(id);
		List<CommentDO> commentList = commentDao.list(commentDO, null);
		if (commentList.size() > 5) {
			commentList = commentList.subList(0, 5);
		}
		map.addAttribute("commentList", commentList);
		
		// 该用户关注的人
		UsrContactDO usrContactDO = new UsrContactDO();
		usrContactDO.setUserId(id);
		ContactResult contactResult = contactDao.byId(usrContactDO);
		if (contactResult.isSuccess()) {
			map.addAttribute("contactList", contactResult.getContactList());
		}
		
		// 用户是否关注该人
		UsrContactDO usrContactDO2 = new UsrContactDO();
		map.addAttribute("isContact", "false");
		Usr usr = (Usr) request.getSession().getAttribute("user");
		if (usr != null) {
			usrContactDO2.setUserId(usr.getId());
			usrContactDO2.setContactId(id);
			ContactResult contactResult2 = contactDao.byId(usrContactDO2);
			if (contactResult2.isSuccess() && contactResult2.getContactList().size() > 0) {
				map.addAttribute("isContact", "true");
			} 
		}
		
		return "user/user";
	}
	
	@Autowired
	private IUserDao userDao;
	
	@Autowired
	private IArticleDao articleDao;
	
	@Autowired
	private ICommentDao commentDao;
	
	@Autowired
	private IContactDao contactDao;

	public ICommentDao getCommentDao() {
		return commentDao;
	}

	public void setCommentDao(ICommentDao commentDao) {
		this.commentDao = commentDao;
	}

	public IContactDao getContactDao() {
		return contactDao;
	}

	public void setContactDao(IContactDao contactDao) {
		this.contactDao = contactDao;
	}

	public IArticleDao getArticleDao() {
		return articleDao;
	}

	public void setArticleDao(IArticleDao articleDao) {
		this.articleDao = articleDao;
	}

	public IUserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}
}
